(function() {
     function mytrim(str)  
    {  
        return str.replace(/(^\s*)|(\s*$)/g, "");  
    } 
	var jiakai_obj={
			'text':'',
			'cNull':'&ensp;&ensp;&ensp;&ensp;',
			'hr':'<HR>',
			'eNull':' ',
			'pStart':'<p>',
			'pEnd':'</p>',
			'isImg':function(){
				var tmp = this.text.replace(/<(?:img).*?>/ig, '');
				if(tmp.length==this.text.length){
					return false;
				}else{
					return true;
				}			
			}, 
			'isObject':function(){
				var tmp = this.text.replace(/<(?:object).*?>/ig, '');
				if(tmp.length==this.text.length){				
					return false;
				}else{
					return true;
				}
							
			}, 
			'mytrim': function(){
				
			},
			'isEmbed':function(){
				var tmp = this.text.replace(/<(?:embed).*?>/ig, '');
				if(tmp.length==this.text.length){
					return false;
				}else{
					return true;
				}		
			}, 
			'isTop':function(){
				var tmp = this.text.indexOf('点击图片进入下一页');//.replace(/<(?:点击图片进入下一页).*?>/ig, '');
				if(tmp<10&&tmp>-1){
					return true;
				}else{
					return false;
				}			
			},
			'trim':function(){
				this.text=this.text.replace(/(^\s.*?)/g, "");
				//this.text=this.text.replace(/(^ .*?)/ig, '');
				//this.text=this.text.replace(/\s/ig,'');
				this.text=this.text.replace('　　','');
			},
			'normalText':function(){
				if(mytrim(this.text)=='<HR>'){}
				else{
				    // this.text=   '<p>'+this.cNull+this.text+'</p>\n';
				    this.text=   '<p style="text-indent:2em;margin-top:13px;">'+this.text+'</p>\n';
				}
			},
			'centerText':function(){
				// this.text=  '<div align="center"><p>'+this.text+'</p></div>';
				this.text=  '<p style="margin-top:13px;">'+this.text+'</p>';
			},		
			'removeNormal':function(){
				//保留 img embed a script strong hr br style
				this.text= this.text.replace(/<(?!img|embed|a|\/a|script|\/script|strong|\/strong|hr|br|style|\/style|object|\/object).*?>/ig, '');
			},
			'replaceHr':function(){
				this.text= this.text.replace(/<(?:hr|\/hr).*?>/ig, '<p><hr /></p>');			
			},
			'replaceBr':function(){
				this.text= this.text.replace(/<(?:br).*?>/ig, '\n\r');
			},
			'removeSpace':function(){
				this.text= this.text.replace(/&nbsp;/ig, '');
			},
			'removeLinks':function(){
				this.text= this.text.replace(/<(?:a|\/a).*?>/ig, '');
			},
			'removeScript':function(){
				this.text= this.text.replace(/<(?:script).*?(?:\/script)>/ig, '');
			},
			'removeStyle':function(){
				this.text= this.text.replace(/<(?:style).*?(?:\/style)>/ig, '');
			},
			'removeDiv':function(){
				this.text= this.text.replace(/<(?:div|\/div).*?>/ig, '');
			},
			'removeP':function(){
				this.text= this.text.replace(/<(?:p|\/p).*?>/ig, '');
			},
			'clearImgAttr':function(){
				//alert("a");
				var str = this.text ;// '<img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" src="http://cms2.dahe.cn/W020100728324324066519.jpg" oldsrc="W020100728324324066519.jpg" _fcksavedurl="/webpic/W0201007/W020100728/W020100728324324066519.jpg" /><img src="efgh"  />';
				var str2 = str;
				var arr,count = 0;
				var reg2 =/(<img\s.*?src=(.*?){0,1}\s.*?>)/gi;
				while ((arr = reg2.exec(str))  != null) {
					var t1 = RegExp.$1; 
					var t2 = RegExp.$2;
					str2=str2.replace(t1,"<img style=\"width:100%;\" src="+t2+" />");
					//alert(str2);				
				}
				this.text = str2;

			},
			'removeLine':function(){
				//过滤掉换行符号
				//alert(this.text.indexOf('\n'));
				this.text=this.text.replace('\n', '');
				this.text=this.text.replace('\r', '');
				//this.text=this.text.replace(/\r\n/ig, '');
			},
			'isNull':function(){
				if(mytrim(this.text)=='')
					return true;
				else
					return false;
			},
			'qingParty':function(){
				this.trim();
				
				if(!this.isNull()){
					this.text=this.pStart+this.text+this.pEnd+'\n';
					// this.text=this.pStart+this.cNull+this.text+this.pEnd+'\n';
				}			
			}
	};
    CKEDITOR.plugins.add('myformat', {
        requires: ['button'],
        init: function(a) {
            a.addCommand('myformat', CKEDITOR.plugins.myformat.commands.myformat);
            a.ui.addButton('myformat', {
                label: "排",
                command: 'myformat',
                icon: this.path + "pai.png"//这个g.ico是你的插件图标，放在同目录下
            });
        }
    });
    CKEDITOR.plugins.myformat = {
        commands: {
            myformat: {
                exec: function(a) {
                    var _html = a.getData();
        			var paiObj=jiakai_obj;
        			paiObj.text=_html;
        			
        			var hrArr=paiObj.text.split('<HR>');
        			var tmpData="";
        			for(j=0;j<hrArr.length;j++){
        				paiObj.text=hrArr[j];
        				var dataArr = paiObj.text.split('</p>');
        				var endData='';
        				var i=0;
        				var textNeedCenter=false;
        				for(i=0;i<dataArr.length;i++){
        					paiObj.text=dataArr[i];
        					paiObj.removeP();
        					paiObj.removeDiv();
        					paiObj.trim();
        					paiObj.removeLine();
        					if(!paiObj.isNull()){
        						if(paiObj.isTop()){
        							// paiObj.centerText();
									paiObj.normalText();
        						}else if(paiObj.isImg()||paiObj.isObject()||paiObj.isEmbed()){
        							paiObj.centerText();
        							textNeedCenter=true;
        						}else if(textNeedCenter){
        							// paiObj.centerText();
									paiObj.normalText();
        							textNeedCenter=false;
        						}else{
        							paiObj.normalText();
        						}
        						endData+=paiObj.text;
        					}	
        					
        				}
        				if((j+1)==hrArr.length){
        					tmpData+=endData;
        				}else{
        					tmpData+=endData+paiObj.hr;
        				}
        			}
                       a.setData(tmpData);
                }
            }
        }
    };
})();
